import networkx as nx
import numpy as np
from numpy import inf

edge = [
    [0, 4, inf, inf, inf, inf, inf, 8 , inf],
    [4, 0, 8,   inf, inf, inf, inf, 3,  inf],
    [inf, 8, 0, 7 ,  inf , 4 , inf, inf , 7],
    [inf, inf, 7, 0, 9, 14,    inf, inf,inf],
    [inf, inf, inf, 9, 0, 10,  inf, inf,inf],
    [inf, inf,   4,14,10,0,2,       inf,inf],
    [inf, inf, inf, inf,inf,    2, 0, 6,  6],
    [8  ,3,  inf, inf, inf, inf,    6, 0, 1],
    [inf, inf, 2, inf, inf  ,inf, 6, 1, 0]
]
new_edge = np.array(edge)
# 若G是无向图
G = nx.Graph()
l = len(new_edge)
for i in range(l):
    for j in range(l):
        if new_edge[i][j] < inf:
            G.add_edge(i, j, weight = new_edge[i][j])
nx.draw(G, with_labels = True)

p = nx.shortest_path(G)
length = dict(nx.shortest_path_length(G))
# print(p)
for i in range(l):
    for j in range(l):
        if p[i][j] :
            print(" 点 %d 到 点 %d的距离是:%d"%(i, j, length[i][j]), p[i][j])
